﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HBKY.Shop.Common.Extension
{
    public static class EnumerableExtension
    {
        /// <summary>
        /// 字典取值
        /// </summary>
        public static bool StringEmpty(this Dictionary<string, string> dic, string key)
        {
            return dic.ContainsKey(key) && !string.IsNullOrEmpty(dic[key]);
        }

        /// <summary>
        /// 判断集合是否为空(true表示有数据)
        /// </summary>
        public static bool HasContent<T>(this IEnumerable<T> enumerable)
        {
            return enumerable != null && enumerable.Any();
        }

        /// <summary>
        /// 拼装集合
        /// </summary>
        /// <param name="pkValues"></param>
        /// <returns></returns>
        public static IEnumerable<int> AddRange(params IEnumerable<int>[] pkValues)
        {
            List<int> list = new List<int>();

            if (pkValues.HasContent())
            {
                foreach (var item in pkValues)
                {
                    if (item.HasContent())
                    {
                        foreach (var _item in item)
                        {
                            if (_item != 0) list.Add(_item);
                        }
                    }
                }
            }

            return list.Distinct();
        }


        /// <summary>
        /// SafeGet
        /// </summary>
        public static TValue SafeGet<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, Func<TValue> func = null) where TValue : new()
        {
            try
            {
                if (dic == null) return func == null ? new TValue() : func();
                if (dic.ContainsKey(key)) return dic[key];
                return func == null ? new TValue() : func();
            }
            catch (System.Exception ex)
            {
                LogHelper.LogManagerNlog.LogError(ex, "SafeGet出错");
                return func == null ? new TValue() : func();
            }
        }

        /// <summary>
        /// SafeGet
        /// </summary>
        public static string SafeGet(this Dictionary<string, string> dic, string key)
        {
            try
            {
                if (dic == null) return string.Empty;
                if (dic.ContainsKey(key)) 
                    return dic[key];
                else
                    return string.Empty;
            }
            catch (System.Exception ex)
            {
                LogHelper.LogManagerNlog.LogError(ex, "SafeGet出错");
                return string.Empty;
            }
        }

        /// <summary>
        /// 去重复
        /// </summary>
        public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            HashSet<TKey> seenKeys = new HashSet<TKey>();
            foreach (TSource element in source)
            {
                if (seenKeys.Add(keySelector(element)))
                {
                    yield return element;
                }
            }
        }

        /// <summary>
        /// 遍历
        /// </summary>
        public static async Task ForEachAsync<T>(this List<T> list, Func<T, Task> func)
        {
            foreach (T value in list)
            {
                await func(value);
            }
        }

        /// <summary>
        /// 将数组的某一索引位置的元素移动到指定索引位置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array"></param>
        /// <param name="fromIndex">要移动的元素所在的索引位</param>
        /// <param name="toIndex">要移动到的索引位</param>
        public static void ArrayMove<T>(ref T[] array, int fromIndex, int toIndex)
        {
            if (
            fromIndex > array.Length - 1 ||
            toIndex > array.Length - 1 ||
            fromIndex == toIndex
            ) return;

            T[] tempArray = new T[array.Length];
            if (fromIndex > toIndex)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    if (i == toIndex)
                    {
                        tempArray[i] = array[fromIndex];
                    }
                    else
                    {
                        if (i > fromIndex || i < toIndex)
                        {
                            tempArray[i] = array[i];
                        }
                        else
                        {
                            tempArray[i] = array[i - 1];
                        }
                    }
                }
            }
            else if (fromIndex < toIndex)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    if (i == toIndex)
                    {
                        tempArray[i] = array[fromIndex];
                    }
                    else
                    {
                        if (i < fromIndex || i > toIndex)
                        {
                            tempArray[i] = array[i];
                        }
                        else
                        {
                            tempArray[i] = array[i + 1];
                        }
                    }
                }
            }
            array = tempArray;
        }
    }
}